package com.example.tms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.tms.dto.EmployeeRelationDTO;
import com.example.tms.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {

    // 1. 查询员工上级关系
    @Select("""
        SELECT ed1.emp_id as employeeId, ed1.superior_id as managerId
        FROM employee_department ed1
        WHERE ed1.is_deleted = 0""")
    List<EmployeeRelationDTO> findAllManagerRelations();

    // 2. 查询项目协作关系
    @Select("""
    SELECT ep1.emp_id as employee1, ep2.emp_id as employee2 ,ep1.pro_id as projectId
    FROM employee_project ep1
    JOIN employee_project ep2 ON ep1.pro_id = ep2.pro_id 
    WHERE ep1.emp_id < ep2.emp_id""")
    List<Map<String, Object>> findAllCollaborations();
    // 3. 查询同事关系
    @Select("""
    SELECT 
        e1.emp_id as employee1,
        e2.emp_id as employee2,
        e1.dep_id as departmentId
    FROM employee_department e1
    JOIN employee_department e2 ON e1.dep_id = e2.dep_id
    WHERE e1.emp_id < e2.emp_id AND e1.is_deleted = 0 AND e2.is_deleted = 0""")
    List<Map<String, Object>> findAllColleagueRelations();


    @Results({
        @Result(property = "id", column = "emp_id", id = true),
        @Result(property = "name", column = "emp_name"),
        @Result(property = "empType", column = "emp_type")
    })
    @Select("SELECT * FROM employee WHERE phone = #{phone} AND is_deleted = 0")
    Employee selectOneByPhone(String phone);

    @Select("INSERT INTO employee_project(emp_id, pro_id) VALUES(#{employeeId}, #{projectId})")
    void insertProjectParticipation(Integer employeeId, Integer projectId);
}
